package com.yunxin.core.infra.persistence.repository.popup;

import com.yunxin.core.domain.model.popup.Popup;
import com.yunxin.core.domain.model.popup.PopupRepository;
import com.yunxin.core.infra.persistence.converter.PopupConverter;
import com.yunxin.core.infra.persistence.entity.popup.PopupDO;
import com.yunxin.core.infra.persistence.mapper.popup.PopupExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-12-11
 */
@Repository
@RequiredArgsConstructor
public class PopupRepositoryImpl implements PopupRepository {

    private final PopupConverter popupConverter;

    private final PopupExtendMapper popupExtendMapper;


    @Override
    public boolean savePopup(Popup popup) {
        PopupDO popupDO = popupConverter.formPopup(popup);
        int result = popupExtendMapper.insertSelective(popupDO);
        return result > 0;
    }

    @Override
    public boolean updatePopup(Popup popup) {
        PopupDO popupDO = popupConverter.formPopup(popup);
        int result = popupExtendMapper.updateByPrimaryKeySelective(popupDO);
        return result > 0;
    }

    @Override
    public Popup getById(Long id) {
        PopupDO popupDO = popupExtendMapper.selectByPrimaryKey(id);
        return popupConverter.toPopup(popupDO);
    }

    @Override
    public Popup getByPopupName(String popupName) {
        PopupDO popupDO = popupExtendMapper.selectPopupByPopupName(popupName);
        return popupConverter.toPopup(popupDO);
    }
}
